# Start-TCL: n
#

#
# Race System v0.1 by Snake
#

package require sqlite3
sqlite3 snk "saves/snk.db3"

namespace eval SnkRace {
	
	variable Enabled 1
	variable Money_Rate 2
		
	variable VERSION 0.1
	variable start 0

	proc GmMenu { player cargs } {
		if { [GetPlevel $player] < 2 } { return "Sie sind nicht dazu berechtigt diesen Befehl zu benutzen!" }
		set event [lindex $cargs 0]
		set mode [lindex $cargs 1]
		set cargs [lindex $cargs 2 end]
		switch $event {
			"sql" { return [SQL $player $mode $cargs] }
		}
	}
	
	proc GossipHello { npc player } {
		set entry [GetEntry $npc]
		switch $entry {
			4445 {
						if { [RaceIsOn] } { Say $npc 0 "A race has started, please wait until finish"; SendGossipComplete $player $npc } else {
						if { [BetOnThisRace $player] } { Say $npc 0 "You have already bet on this race"; SendGossipComplete $player $npc } else {
						if { [AlreadyBet $player] } { Say $npc 0 "You have an old bet, please claim your reward and then bet again";  SendGossipComplete $player $npc } else {
						SendGossip $player $npc { text 0 "100 Gold" }\
						SendGossip $player $npc { text 0 "10 Gold" }\
						SendGossip $player $npc { text 0 "1 Gold" }\
						SendGossip $player $npc { text 0 "10 Silber" }\
						SendGossip $player $npc { text 0 "1 Silber" }\
						SendGossip $player $npc { text 0 "10 Kupfer" }\
						SendGossip $player $npc { text 0 "1 Kupfer" }\
						}}}
					}
			4419 {
						if { [RaceIsOn] } { Say $npc 0 "Ein Rennen ist bereits gestartet, bitte warte bis es vorbei ist." } else {
						if { [BetOnThisRace $player] } { Say $npc 0 "Das Rennen hat noch nicht angefangen."; SendGossipComplete $player $npc } else {
						if { [AlreadyBet $player] != 1 } { Say $npc 0 "Du hast auf kein Rennen gesetzt." } else {
						SendGossip $player $npc { text 0 "Pruefe meinen Wetteinsatz." }
						}}}
					}
				}
			}
	
	proc GossipSelect { npc player option } {
		set entry [GetEntry $npc]
		switch $entry {
			4445 {
						set money [GetMoney $option]
						if { ![ChangeMoney $player -$money] } { Say $npc 0 "Du hast nicht genug Geld!"; SendGossipComplete $player $npc } else {
						CreateBet $player $money
						SendGossipComplete $player $npc
						Say $npc 0 "Vielen Dank fuer deinen Wetteinsatz."
						}
					}
			4419 {
						set name [GetName $player]
						set rn [snk eval { SELECT `racenumber` FROM `bets` WHERE (`madeby` = $name) }]
						if { [GetSide $player] != [GetWinner $rn] } { Say $npc 0 "Du hast verloren."; DeleteBet $player; SendGossipComplete $player $npc } else {
						snk eval { SELECT * FROM `bets` WHERE (`madeby` = $name) } {
							set money [expr $money * $SnkRace::Money_Rate]
							ChangeMoney $player +$money
							Say $npc 0 "Glueckwunsch, du hast gewonnen!"
						}
							DeleteBet $player
							SendGossipComplete $player $npc 
						}
					}
				}
			}
	
	proc CreateBet { player money } {
		set name [GetName $player]
		snk eval { SELECT `rn` FROM `races` ORDER BY `entry` DESC LIMIT 1 } {
			set rn [expr $rn + 1]
			snk eval { INSERT INTO `bets` (`racenumber`, `money`, `madeby`) VALUES($rn, $money, $name) }
		}
		puts "[Custom::LogPrefix] $name Setzt auf Rennen $money Kupfer"
	}
	
	proc DeleteBet { player } {	set name [GetName $player];	snk eval { DELETE FROM `bets` WHERE (`madeby` = $name) } }
	proc GetWinner { rn } { snk eval { SELECT `winner` FROM `races` WHERE (`rn` = $rn) } { return $winner } }
	proc StartRace { npc } { snk eval { SELECT `rn` FROM `races` ORDER BY `entry` DESC LIMIT 1 } { set rn [expr $rn + 1] }; snk eval { INSERT INTO `races` (`rn`, `finish`) VALUES($rn, "no") };	variable start; set start 1 }
	proc FinishRace { npc } { set finish [snk eval { SELECT `finish` FROM `races` ORDER BY `entry` DESC LIMIT 1 }]; if { $finish == "yes" } { return } else { set entry [GetEntry $npc]; set side [GetNpcSide $entry]; set rn [snk eval { SELECT `rn` FROM `races` ORDER BY `entry` DESC LIMIT 1 }]; snk eval { UPDATE `races` SET `finish` = "yes", `winner` = $side WHERE (`rn` = $rn ) }; variable start; set start 0 } }
	proc RaceIsOn {} { variable start; return $start }
	proc BetOnThisRace { player } {	set name [GetName $player];	snk eval { SELECT `rn` FROM `races` ORDER BY `entry` DESC LIMIT 1 } { set rn [expr $rn + 1]; if { [ string length [ snk eval { SELECT `madeby` FROM `bets` WHERE (`racenumber` = $rn AND `madeby` = $name) } ] ] } { return 1 } }; return 0 }
	proc AlreadyBet { player } { set name [GetName $player]; if { [ string length [ snk eval { SELECT `madeby` FROM `bets` WHERE (`madeby` = $name) } ] ] } { return 1 }; return 0 }	
	
	proc SQL { player mode cargs } {
		switch $mode {
			"reset" { DeleteTables; CreateTables; return "Race Tabelle zurueckgesetzt." }
			"cleanup" { [CleanupTables]; return "Rennen DB gesaeubert." }
			default { return "Modi sind zurueckgesetzt/gesaeubert." }
		}
	}
	
	proc CreateTables {} {
		if { [ string length [ snk eval { SELECT `name` FROM `sqlite_master` WHERE (`type` = 'table' AND `tbl_name` = 'races') } ] ] } { return	}
		snk eval { CREATE TABLE `races` (`entry` INTEGER PRIMARY KEY AUTOINCREMENT, `winner` TEXT NOT NULL DEFAULT '', `rn` TEXT NOT NULL DEFAULT 0, `finish` TEXT NOT NULL DEFAULT 'no') }
		snk eval { CREATE TABLE `bets` (`entry` INTEGER PRIMARY KEY AUTOINCREMENT, `racenumber` TEXT NOT NULL DEFAULT 0, `money` TEXT NOT NULL DEFAULT 0, `madeby` TEXT NOT NULL DEFAULT '') }
		snk eval { INSERT INTO `races` (`winner`, `rn`, `finish`) VALUES("bla", 0, "no") }
	}
	
	proc DeleteTables {} {
		if { ![ string length [ snk eval { SELECT `name` FROM `sqlite_master` WHERE (`type` = 'table' AND `tbl_name` = 'races') } ] ] } { return }
		snk eval { DROP TABLE `races` }
		snk eval { DROP TABLE `bets` }
		snk eval { VACUUM }
	}
	
	proc CleanupTables {} {
		snk eval { VACUUM }
	}
	
	proc GetMoney { option } {
		switch $option {
			0 { return 1000000 }
			1 { return 100000 }
			2 { return 10000 }
			3 { return 1000 }
			4 { return 100 }
			5 { return 10 }
			6 { return 1 }
		}
	}
	
	proc GetNpcSide { entry } {
		switch $entry {
			4251 { return "Horde" }
			4252 { return "Allianz" }
		}
	}
	
	proc GetSide { player } {
		set race [GetRace $player]
		switch $race {
			1 { return "Allianz" }
			3 { return "Allianz" }
			4 { return "Allianz" }
			7 { return "Allianz" }
			2 { return "Horde" }
			5 { return "Horde" }
			6 { return "Horde" }
			8 { return "Horde" }
		}
	}
	
	SnkRace::CreateTables
	Custom::AddCommand "race" "SnkRace::GmMenu"
	
	puts "[Custom::LogPrefix] Snk Race System v$VERSION Loaded"	
}